package br.com.manutencaocarro;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.widget.ArrayAdapter;
import android.widget.Spinner;
import br.com.manutencaocarro.activity.CadastraCarroActivity;
import br.com.manutencaocarro.modelo.Carro;
import br.com.manutencaocarro.modelo.Montadora;
import br.com.manutencaocarro.utils.Constants;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class ColetaMontadoras 
{
	public List<Montadora> montadoras;
	public List<Carro> carros;
	private static Carro carro;
	private static Montadora montadora;
	private static Spinner sMontadora;
	private static Spinner sCarros;

	public ColetaMontadoras() {
	}

	public void invokeWSMontadoras( RequestParams params, final CadastraCarroActivity cadastraCarroActivity )
	{
		montadoras = new ArrayList<>();
		AsyncHttpClient client = new AsyncHttpClient();
		client.get( Constants.URL_BASE + "ManutencaoCarroWebService/montadoras", params ,new AsyncHttpResponseHandler() {
			// When the response returned by REST has Http response code '200'
			@Override
			public void onSuccess(String response) {
				// Hide Progress Dialog
				try {
					// JSON Object
					JSONObject obj = new JSONObject(response);

					if( obj.length() > 0 )
					{
						for( int i = 0; i <= obj.length(); i++ )
						{
							montadora = new Montadora();
							montadora.setIdMontadora( obj.getJSONArray("montadora").getJSONObject(i).getInt("idMontadora") );
							montadora.setNomeMontadora( obj.getJSONArray("montadora").getJSONObject(i).getString("nomeMontadora") );
							montadoras.add(montadora);
						}

						ArrayAdapter<Montadora> adapter = new ArrayAdapter<>( cadastraCarroActivity, android.R.layout.simple_spinner_item, montadoras);
						adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
						sMontadora = (Spinner) cadastraCarroActivity.findViewById(R.id.montadora);
						sMontadora.setAdapter(adapter);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			// When the response returned by REST has Http response code other than '200'
			@Override
			public void onFailure(int statusCode, Throwable error, String content) {
				// Hide Progress Dialog
				// When Http response code is '404'
				if(statusCode == 404){
				} 
				// When Http response code is '500'
				else if(statusCode == 500){
				} 
				// When Http response code other than 404, 500
				else{
				}
			}
		});
	}

	public void invokeWSCarrosMontadoras( RequestParams params, final CadastraCarroActivity cadastraCarroActivity )
	{
		carros = new ArrayList<>();
		AsyncHttpClient client = new AsyncHttpClient();
		client.get( Constants.URL_BASE + "ManutencaoCarroWebService/montadoras/carros", params ,new AsyncHttpResponseHandler() {
			// When the response returned by REST has Http response code '200'
			@Override
			public void onSuccess(String response) 
			{
				// Hide Progress Dialog
				try {
					// JSON Object
					JSONObject obj = new JSONObject(response);

					if( obj.length() > 0 )
					{
						try {
							carro = new Carro();
							carro.setId(obj.getJSONObject("carro").getInt("idCarro"));
							carro.setModelo(obj.getJSONObject("carro").getString("modeloCarro"));
							carro.setIdMontadora(obj.getJSONObject("carro").getInt("idMontadora"));
							carros.add( carro );
						} catch( Exception e ) 
						{
							JSONArray ja = obj.getJSONArray("carro");
							for( int i = 0; i < ja.length(); i++ )
							{
								carro = new Carro();
								carro.setId(ja.getJSONObject(i).getInt("idCarro"));
								carro.setModelo(ja.getJSONObject(i).getString("modeloCarro"));
								carro.setIdMontadora(ja.getJSONObject(i).getInt("idMontadora"));
								carros.add( carro );
							}
						}

						ArrayAdapter<Carro> adapter = new ArrayAdapter<>( cadastraCarroActivity, android.R.layout.simple_spinner_item, carros);
						adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
						sCarros = (Spinner) cadastraCarroActivity.findViewById(R.id.modelocarro);
						sCarros.setAdapter(adapter);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			// When the response returned by REST has Http response code other than '200'
			@Override
			public void onFailure(int statusCode, Throwable error, String content) {
				// Hide Progress Dialog
				// When Http response code is '404'
				if(statusCode == 404){
				} 
				// When Http response code is '500'
				else if(statusCode == 500){
				} 
				// When Http response code other than 404, 500
				else{
				}
			}
		});
	}
}